package cn.itsource.org.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.org.domain.ProductComment;
import cn.itsource.org.domain.ProductDetail;
import cn.itsource.org.mapper.ProductCommentMapper;
import cn.itsource.org.query.ProductCommentQuery;
import cn.itsource.org.query.ProductDetailQuery;
import cn.itsource.org.service.IProductCommentService;
import cn.itsource.org.service.IProductDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ProductCommentServiceImpl extends BaseServiceImpl<ProductComment, ProductCommentQuery> implements IProductCommentService {

    @Resource
    private ProductCommentMapper productCommentMapper;
    @Override
    public List<ProductComment> findPage() {
        List<ProductComment> productComments = productCommentMapper.selectAll();
        List<ProductComment> productCommentes = getProductComments(productComments);
        return productCommentes;
    }

    private List<ProductComment> getProductComments(List<ProductComment> productComments) {
        List<String> dirPaths = productComments.stream().map(ProductComment::getDirPath).collect(Collectors.toList());
        //获取所有的dirpath
        for (String dirPath : dirPaths) {
            //把所有的dirpath进行分割
            String[] split = dirPath.split("/");

            String[] strings = Arrays.copyOfRange(split, 1, split.length);
            //转为数字数组
            int[] ints = Arrays.stream(strings).mapToInt(Integer::parseInt).toArray();
           // System.out.println(Arrays.toString(ints));
            //获取数组的最原始的评论
            int anInt1 = ints[0];
            ProductComment productComment1 = productCommentMapper.selectByPrimaryKey(anInt1);
            StringBuilder stringBuilder = new StringBuilder();
            //获取到所有评论地址集合
            for (int anInt : ints) {
                //根据每个页面查评论
                ProductComment productComment = productCommentMapper.selectByPrimaryKey(anInt);
                //最原始的评论拼接
                stringBuilder.append(productComment.getComment());
            }
            String s = stringBuilder.toString();
            //设置评论
            productComment1.setComments(s);
            //升级本条数据
            productCommentMapper.updateByPrimaryKeySelective(productComment1);
        }
        //再查一次
        return productCommentMapper.selectAll();
    }
}
